package com.hfzy.ihk.web.linkcall.controller.base;

import com.hfzy.ihk.common.util.Utils;
import com.hfzy.ihk.common.web.WebResult;
import com.hfzy.ihk.facade.linkcall.entity.BusinessTypeBase;
import com.hfzy.ihk.web.linkcall.biz.BusinessTypeBaseBiz;
import com.hfzy.ihk.web.linkcall.utils.UserUtils;
import com.hfzy.ihk.web.linkcall.utils.WebResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/businessTypeBase")
public class BusinessTypeBaseController {
    @Autowired
    private BusinessTypeBaseBiz businessTypeBaseBiz;

    /**
     * 页面
     * */
    @RequestMapping("/saveOrUpdateBusinessTypeBase")
    public String saveOrUpdate(String id,Model model){
        List<BusinessTypeBase> businessTypeBases = businessTypeBaseBiz.getAll();
        if(!Utils.isNullString(id)){
            BusinessTypeBase businessTypeBase = businessTypeBaseBiz.selectById(id);
            if(businessTypeBase!=null){
                businessTypeBases.remove(businessTypeBase);
                model.addAttribute("businessTypeBase",businessTypeBase);
            }
        }
        return "page/businessTypeBase/saveOrUpdateBusinessTypeBase";
    }

    /**
     * 类型页面
     * */
    @RequestMapping("/queryBusinessTypeBase")
    public String query(){
        return "page/businessTypeBase/queryBusinessTypeBase";
    }


    /**
     * ======================================业务操作==============================================
     * */
    @ResponseBody
    @RequestMapping("/queryByPage")
    public WebResult queryByPage(@RequestParam Map<String,String> params){
        WebResult webResult = null;
        try {
            Page<BusinessTypeBase> page = businessTypeBaseBiz.getByPage(params);
            List<Map> list = new ArrayList<Map>();

            if(page.getContent()!=null && !page.getContent().isEmpty()){
                List<String> uIds = page.getContent().stream().map(it->it.getCreateUserId()+"").collect(Collectors.toList());
                Map<Integer,String> userMap = UserUtils.getUserNameMap(uIds);
                for(BusinessTypeBase industryType:page.getContent()){
                    Map<String,Object> map = new HashMap<>();
                    map.put("name",industryType.getName());
                    map.put("id",industryType.getId());
                    map.put("createUserName",userMap.get(industryType.getCreateUserId())!=null?userMap.get(industryType.getCreateUserId()):"系统管理员");
                    map.put("modTime",Utils.date2DateTimeString(industryType.getModTime()));
                    list.add(map);
                }
            }
            webResult = WebResultUtils.getSuccessCharWebResult("",page.getTotalElements(),list);
        }catch (Exception e){
            e.printStackTrace();
            webResult = WebResultUtils.getExceptionCharWebResult("");
        }finally {
            if(webResult==null)
                webResult = WebResultUtils.getFailCharWebResult("");
        }
        return webResult;
    }


    @ResponseBody
    @RequestMapping("/save")
    public WebResult save(@RequestParam Map<String, String> params){
        WebResult webResult= null;
        try{
            if(params!=null && !params.isEmpty()){
                BusinessTypeBase businessTypeBase = new BusinessTypeBase();
                Iterator<Map.Entry<String,String>> it = params.entrySet().iterator();
                businessTypeBase.setName(params.get("name"));
                if(params.containsKey("id") && !Utils.isNullString(params.get("id"))){
                    businessTypeBase.setId(params.get("id"));
                }
                webResult = businessTypeBaseBiz.save(businessTypeBase);
            }
        }catch (Exception e){
            webResult = WebResultUtils.getExceptionCharWebResult("");
        }finally {
            if(webResult==null)
                webResult = WebResultUtils.getFailCharWebResult("参数异常");
        }
        return webResult;
    }

    @ResponseBody
    @RequestMapping("/del")
    public WebResult del(@RequestParam String id){
        WebResult webResult = businessTypeBaseBiz.deleteBusinessTypeBaseById(id);
        return webResult;
    }

}
